#include "Private.h"
#include "Network.h"
#include <linux/net.h>
#include <linux/types.h>
#include <linux/socket.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <linux/unistd.h>
#include <Syscall.h>

/// <summary>
/// The NetLinkOpen function
/// open a NETLINK socket.
/// </summary>
int NetLinkOpen(void)
{
	int handle = -1;
	struct sockaddr_nl nladdr;

	if ((handle = CreateSocket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) < 0)
		return -1;

	/* Fill structure */
	nladdr.nl_family = AF_NETLINK;
	nladdr.nl_pad = 0;
	nladdr.nl_pid = 0;
    nladdr.nl_groups = 0;

	if (NetBind(handle, (struct sockaddr *)&nladdr, sizeof(nladdr)) < 0) {
		CloseSocket(handle);
		return -1;
	}

	return handle;
}

